package com.yt.order.api.client;

import com.yt.order.common.config.WxPayConfig;
import com.yt.order.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/wxpay")
public class WxPayController {
    
    @Autowired
    private WxPayService wxPayService;
    
    @Autowired
    private WxPayConfig wxPayConfig;
    
    /**
     * 发起微信支付
     * @param request 支付请求参数
     * @return 支付参数
     */
    @PostMapping("/unifiedOrder")
    public Map<String, Object> unifiedOrder(@RequestBody UnifiedOrderRequest request) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            log.info("开始统一下单，请求参数: {}", request);
            
            // 为了简化支付流程，直接返回模拟的支付参数
            // 在实际项目中，这里会调用微信支付统一下单接口
            
            // 模拟生成支付参数
            Map<String, String> paySign = new HashMap<>();
            paySign.put("appId", wxPayConfig.getAppId());
            paySign.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
            paySign.put("nonceStr", wxPayService.generateNonceStr());
            paySign.put("package", "prepay_id=wx123456789012345678901234567890");
            paySign.put("signType", "MD5");
            paySign.put("paySign", "mock_pay_sign_123456");
            
            result.put("code", 0);
            result.put("data", paySign);
            result.put("msg", "success");
        } catch (Exception e) {
            log.error("统一下单失败", e);
            result.put("code", -1);
            result.put("msg", "统一下单失败: " + e.getMessage());
        }
        
        return result;
    }
    
    /**
     * 微信支付结果通知
     * @param notifyData 通知数据
     * @return 处理结果
     */
    @PostMapping("/notify")
    public String notify(@RequestBody String notifyData) {
        try {
            // 模拟支付成功处理
            log.info("模拟支付成功，通知数据: {}", notifyData);
            
            // 在实际项目中，这里会解析微信支付通知并更新订单状态
            // 为了简化流程，我们直接返回成功响应
            
            return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        } catch (Exception e) {
            log.error("处理支付通知失败", e);
            return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[处理失败]]></return_msg></xml>";
        }
    }
    
    /**
     * 统一下单请求参数
     */
    public static class UnifiedOrderRequest {
        private String orderId;
        private int amount;
        private String description;
        private String openid;
        
        // getters and setters
        public String getOrderId() {
            return orderId;
        }
        
        public void setOrderId(String orderId) {
            this.orderId = orderId;
        }
        
        public int getAmount() {
            return amount;
        }
        
        public void setAmount(int amount) {
            this.amount = amount;
        }
        
        public String getDescription() {
            return description;
        }
        
        public void setDescription(String description) {
            this.description = description;
        }
        
        public String getOpenid() {
            return openid;
        }
        
        public void setOpenid(String openid) {
            this.openid = openid;
        }
    }
}